<?php
namespace Common\Controller;
use Common\Controller\CommonController;
class IdiomController extends CommonController
{

    /**
     * 缓存题目
     */
    public function setidiomquersion(){

        $question = M('idiom_questions')->field('sort,createtime',true)->select();

        foreach ($question as $key=>$value){
            $question[$key]['imgsrc'] = $this->url.$question[$key]['imgsrc'];
        }

        S('idiomquestion',$question,3600*24*30);

    }

    /**
     * 获取题目缓存并返回随机值
     */
    public function getidiomquersion($num){

        $cache = S('idiomquestion');

        if (!$cache||$cache==""){
            $this->setidiomquersion();
            $cache = S('idiomquestion');
        }

        srand((float) microtime() * 10000000);
        $id = array_rand($cache,$num);
        $data = array();
        for ($i=0;$i<count($id);$i++){
            $data[] = $cache[$id[$i]];
        }

        return $data;

    }

    /**
     * 缓存本周排行
     */
    public function setcacheweek(){

        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =$info->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.getnum,week.idiom_week_id,uesr.nickName,uesr.avatarUrl')->limit(100)->order('week.getnum desc' )->select();
//
//        if ($list) {
//            $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();
//
//            $list[0]['money'] = '预计'.$gift[0]['giftname'];
//            if ($list[1]['idiom_week_id']){
//                $list[1]['money'] = '预计'.$gift[1]['giftname'];
//            }
//            if ($list[2]['idiom_week_id']){
//                $list[2]['money'] = '预计'.$gift[2]['giftname'];
//            }
//
//            if ($list[3]['idiom_week_id']){
//                $list[3]['money'] = '预计'.$gift[3]['giftname'];
//            }
//
//            if ($list[4]['idiom_week_id']){
//                $list[4]['money'] = '预计'.$gift[3]['giftname'];
//            }
//
//            if ($list[5]['idiom_week_id']){
//                $list[5]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[6]['idiom_week_id']){
//                $list[6]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[7]['idiom_week_id']){
//                $list[7]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[8]['idiom_week_id']){
//                $list[8]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//            if ($list[9]['idiom_week_id']){
//                $list[9]['money'] = '预计'.$gift[4]['giftname'];
//            }
//
//
//        }

        S('idiomweek',$list,600);

    }

    /**
     * 缓存本周排行(版本2)
     */
    public function setcacheweek2(){

        $year = date('Y');
        $week = (int)date('W');
        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$week,
        );

        $list =$info->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.customs,week.idiom_week_id,uesr.nickName,uesr.avatarUrl')->limit(100)->order('week.customs desc' )->select();


        S('idiomweekt',$list,600);

    }

    /**
     * 获取本周排行版本2
     * @return mixed|string
     */
    public function getcacheweek2(){

        $cache = "";
        $cache = S('idiomweekt');

        if (!$cache||$cache==""){
            $this->setcacheweek2();
            $cache = S('idiomweekt');
        }
        return $cache;
    }


    /**
     * 获取本周排行
     * @return mixed|string
     */
    public function getcacheweek(){

        $cache = "";
        $cache = S('idiomweek');

        if (!$cache||$cache==""){
            $this->setcacheweek();
            $cache = S('idiomweek');
        }
        return $cache;
    }

    /**
     * 查询上周得奖
     */
    public function setlasetweek(){

        $lastweek = (int)date("W", strtotime("-1 week"));;

        $week = date('W');

        $year = date('Y');
        if ($week=='1'){
            $year= date("Y", strtotime("-1 year"));
        }


        $info = M();

        $where = array(
            'week.year'=>$year,
            'week.week'=>$lastweek,
        );

        $list =$info->table('mj_idiom_week as week')->join('mj_idiom_member as uesr on  week.idiom_member_id = uesr.idiom_member_id')->where($where)->field('week.getnum,week.idiom_week_id,uesr.nickName,uesr.avatarUrl')->limit(10)->order('week.getnum desc' )->select();

        if ($list) {
            $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();

            $list[0]['giftname'] = $gift[0]['giftname'];
            if ($list[1]['idiom_week_id']){
                $list[1]['giftname'] = $gift[1]['giftname'];
            }
            if ($list[2]['idiom_week_id']){
                $list[2]['giftname'] = $gift[2]['giftname'];
            }

            if ($list[3]['idiom_week_id']){
                $list[3]['giftname'] = $gift[3]['giftname'];
            }

            if ($list[4]['idiom_week_id']){
                $list[4]['giftname'] = $gift[3]['giftname'];
            }

           if ($list[5]['idiom_week_id']){
               $list[5]['giftname'] = $gift[4]['giftname'];
           }

            if ($list[6]['idiom_week_id']){
                $list[6]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[7]['idiom_week_id']){
                $list[7]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[8]['idiom_week_id']){
                $list[8]['giftname'] = $gift[4]['giftname'];
            }

            if ($list[9]['idiom_week_id']){
                $list[9]['giftname'] = $gift[4]['giftname'];
            }


        }

        S('lastweek',$list,3600);

    }

    /**
     * 获取上周得奖
     * @return mixed
     */
    public function getlastweek(){

        $cache = S('lastweek');

        if (!$cache||$cache==""){
            $this->setlasetweek();
            $cache = S('lastweek');
        }

        return $cache;
    }


    public function monilastweek(){

        $gift = M('idiom_gift')->field('giftname')->order('rank asc')->select();
        $a1 = array(
            'nickname'=>'小时代',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/8R3rKudsCiciaecdzByO52qxEF9IVrul9yfrdYa7ibmnyhmedxDcdLfYA5FIC9zaDic8ReIsIoJu1NcWnaZYFYFFZg/0',
            'giftname'=>$gift[0]['giftname'],
        );

        $a2 = array(
            'nickname'=>'你好，生活',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/cC0d9lAQ3SI4JJyd3YRoDyCAia4oJwwabGFF3rZDotlGZofKyfptQvcSEz5MEwo2W4AMFVlhXkHvdGiaoSK0oDJA/0',
            'giftname'=>$gift[1]['giftname'],
        );
        $a3 = array(
            'nickname'=>'阿西吧',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLD8ia3yWyiaVcw6wzk9fMeoBcmK4kqRlnNEfyWPUTp05QVvicFWPvO9HYVDs6bzbjfWSDYr3sZsMJNg/0',
            'giftname'=>$gift[2]['giftname'],
        );
        $a4 = array(
            'nickname'=>'生活百态',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/BhKI1CuHnjxHRcuHK5tspzHG7nv8xQfJaKcTxfpoASfDBLQLgdAZTuIqiayDcJpndZJj2ntwibhejStHugHD9ZAw/0',
            'giftname'=>$gift[3]['giftname'],
        );
        $a5 = array(
            'nickname'=>'asdvs',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/0YRSb7hiaGZfYU1zVWg8QUhRnMq7twb2qZVwibs2f4jD2via98bLEcfXEYnN3j5CFRM2U1U4KlxiaiboQugU8VxoSzA/0',
            'giftname'=>$gift[3]['giftname'],
        );
        $a6 = array(
            'nickname'=>'人生如梦',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/gtBia22uicroVwMt22LiamAwwnvzlKggT0Nawd5X6OWrQOCOaDF8w3kngJsRNCnVODqmTuwdk4uficMG1ZiaibjjwC3Q/0',
            'giftname'=>$gift[4]['giftname'],
        );

        $a7 = array(
            'nickname'=>'过往.....',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJPFdtUib2BQHfvB9hfzHKib4hUsa6b0hky1JPwT7Y8UZic4Voevnky45U9XBj4SkSofEhtRiaSoicxkPQ/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a8 = array(
            'nickname'=>'依然记得',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/DYAIOgq83erYibdVmPXiblHv0z4ZPU4d9Xm3MPulWejtmhNQhiahLiaxaNJF0p4oIfBYPWXsu2zAwLqRskscC7h31A/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a9 = array(
            'nickname'=>'流浪.......',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTISJHoK6dDib7A7MP2dhibc7dia4V873OIwfgE285AHOv3gzlD69MF99p9Img7THqzo370GQUNsOfYcg/0',
            'giftname'=>$gift[4]['giftname'],
        );
        $a10 = array(
            'nickname'=>'iuriik',
            'avatarurl'=>'https://wx.qlogo.cn/mmopen/vi_32/HiaasQla0F1YrAUZB7zp9giaUib9jaSEMK4icVDhdiaWx2UFBJbvHH5BAsAsQs35RbdiaX3UgTAmzUST8jjYTXWjkpOQ/0',
            'giftname'=>$gift[4]['giftname'],
        );

        $data = array(
            '0'=>$a1,
            '1'=>$a2,
            '2'=>$a3,
            '3'=>$a4,
            '4'=>$a5,
            '5'=>$a6,
            '6'=>$a7,
            '7'=>$a8,
            '8'=>$a9,
            '9'=>$a10,
        );

        return $data;
    }

//    /**
//     * 获取当天排行
//     */
//    public function getrank1($date,$state){
//
//        $cache = S('date_rank');
//        if ($state!='2'){
//            if (!empty($cache)){
//                return $cache;
//            }
//        }
//
//        if (empty($date)){
//            $date = date('Y-m-d');
//        }
//
//        $ids = M('idiom_chang')->where(array('date'=>$date))->getField('idiom_member_id',true);
//        $data = array();
//        if (!empty($ids)){
//            $ids = array_unique($ids);  //去除重复值
//            $ids = array_merge($ids);  //去下标
//            for ($i=0;$i<count($ids);$i++){
//
//                $where['date'] = $date;
//                $where['idiom_member_id'] = $ids[$i];
//                $info = M('idiom_chang')->field('idiom_chang_id,idiom_member_id',true)->where($where)->order('customs desc')->find();
//                $data[] = $info;
//            }
//            //冒泡
//            $len = count($data);
//            for ($k=0;$k<$len;$k++){
//                for ($j=0;$j<$len-$k;$j++){
//                    if ($data[$j]['customs']<$data[$j+1]['customs']){
//                        $tem = $data[$j];
//                        $data[$j] = $data[$j+1];
//                        $data[$j+1] = $tem;
//                    }
//                }
//
//            }
//            if ($state=='2'){
//
//                if ($data[0]){
//                    $data[0]['giftname'] = '获得奖金100元';
//                }
//                if ($data[1]){
//                    $data[1]['giftname'] = '获得奖金50元';
//                }
//                if ($data[2]){
//                    $data[2]['giftname'] = '获得奖金30元';
//                }
//                $arr = array();
//                $arr['gift'] = array(
//                    '0'=>$data[0],
//                    '1'=>$data[1],
//                    '2'=>$data[2],
//                );
//                $m = date('m',strtotime($date));
//                $d = date('d',strtotime($date));
//                $arr['date'] = (int)$m.'月'.(int)$d.'日';
//                return $arr;
//            }
//
//            if ($data[0]){
//                $data[0]['giftname'] = '预计获得奖金100元';
//            }
//            if ($data[1]){
//                $data[1]['giftname'] = '预计获得奖金50元';
//            }
//            if ($data[2]){
//                $data[2]['giftname'] = '预计获得奖金30元';
//            }
//
//
//        }
//
//        S('date_rank',$data,600);
//        return S('date_rank');
//
//    }

    /**
     * 获取排行
     */
    public function getrank($date,$state){

        $cache = S('date_rank');

        if ($state!='2'){
            if (!empty($cache)){
               return $cache;
            }
        }
        if (empty($date)){
            $date = date('Y-m-d');
        }
        if ($state=='2')
        {
            $data  =   M('idiom_chang')->where(array('date'=>$date))->field("max(customs) as customs,nickname,avatarurl,date")->group('idiom_member_id')->order(array('customs'=>'desc'))->limit(3)->select();
            if ($data[0]){
                $data[0]['giftname'] = '获得奖金100元';
            }
            if ($data[1]){
                $data[1]['giftname'] = '获得奖金50元';
            }
            if ($data[2]){
                $data[2]['giftname'] = '获得奖金30元';
            }
            $arr = array();
            $arr['gift'] = array(
                '0'=>$data[0],
                '1'=>$data[1],
                '2'=>$data[2],
            );
            $m = date('m',strtotime($date));
            $d = date('d',strtotime($date));
            $arr['date'] = (int)$m.'月'.(int)$d.'日';
            return $arr;

        }

        //今日
        $data  =   M('idiom_chang')->where(array('date'=>$date))->field("max(customs) as customs,nickname,avatarurl,date")->group('idiom_member_id')->order(array('customs'=>'desc'))->limit(100)->select();



//        if ($data[0]){
//            $data[0]['giftname'] = '预计获得奖金100元';
//        }
//        if ($data[1]){
//            $data[1]['giftname'] = '预计获得奖金50元';
//        }
//        if ($data[2]){
//            $data[2]['giftname'] = '预计获得奖金30元';
//        }

        S('date_rank',$data,600);
        return S('date_rank');

    }

    //本周排行
    public function getweekrank(){

        $cache = S('idiomweekrank');
        if ($cache){
            return $cache;
        }
        //本周
        $where['week'] = (int)date('W');
        $data  =   M('idiom_chang')->where($where)->field("max(customs) as customs,nickname,avatarurl,date")->group('idiom_member_id')->order(array('customs'=>'desc'))->limit(100)->select();

        S('idiomweekrank',$data,600);

        return S('idiomweekrank');

    }

    private function moni($date){

        $data = array();
        $arr[0] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/chengyu.jpg',
            'giftname'=>'获得奖金100元'
        );
        $arr[1] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/chengyu.jpg',
            'giftname'=>'获得奖金50元'
        );
        $arr[2] = array(
            'nickname'=>'虚位以待',
            'avatarurl'=>$this->url.'appshow/chengyu.jpg',
            'giftname'=>'获得奖金30元'
        );
        $data['gift'] = array(
            '0'=>$arr[0],
            '1'=>$arr[1],
            '2'=>$arr[2],
        );

        $m = date('m',strtotime($date));
        $d = date('d',strtotime($date));
        $data['date'] = (int)$m.'月'.(int)$d.'日';
        return $data;
    }

    /**
     * 近期得奖
     */
    public function gift($state){

        $date1 = '2018-06-16';
        $date2 = '2018-06-17';
        $date3 = '2018-06-18';

        $noedate = date('Y-m-d');


        if ($noedate>$date1){
            $data[0] = $this->getrank($date1,$state);
        }else{
          $data[0] = $this->moni($date1);

        }
        if ($noedate>$date2){
            $data[1] = $this->getrank($date2,$state);
        }else{
            $data[1] = $this->moni($date2);
        }

        if ($noedate>$date3){
            $data[2] = $this->getrank($date3,$state);
        }else{
            $data[2] = $this->moni($date3);
        }


        return $data;

    }

}